-- Beehive
-- Created by chengb Feb/3/2016
-- 蜂巢格子

-- 初始化
function initBeehive(self)
    if self.gridData.state == GRID_STATE_DISABLE and self.gridData.bonus then
        -- 如果有掉落
        self:initItem(self.gridData);
        return;
    end

    -- 查看蜂巢
    local function viewBeehive()
        -- 触发蜂巢事件
        DungeonActionM.go("beehive", self.gridData:getPos());
    end

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 是否禁止使用蜂巢
            if self.gridData.forbidToUseBeeHive == true then
                alert(getLocStr("forbid_to_use_beehive"));
                return;
            end

            -- 音效
            AudioM.playFx("bee1");

            -- 显示建筑查看界面
            local dialog = buildingInfo["dialog"];
            local beehiveData = BeehiveM.getBeehiveData(self.gridData:getPos());
            local curTimes = beehiveData["times"];
            local maxTimes = BeehiveM.query(beehiveData["id"], "times");
            dialog = dialog .. string.format(getLocStr("use_times_format"), curTimes, maxTimes);
            local offsetY = -20;

            self:openBuildingView(viewBeehive, offsetY, dialog);
        end
    end

    -- 蜂巢图标
    local iconPath = getLevelItemIconPath("beehive");

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setName("icon");
    self.icon:setTouchEnabled(true);
    AddTouchEventListener(self.icon, onClicked, true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 密蜂特效
    local node = cc.Node:create();
    self.item:addChild(node);
    node:setName("bee_effect");
    playEffect(node, 1137);

    -- 是否闪光
    if buildingInfo["shinning"] == 1 then
        self.shiningAjustPos = cc.p(70, 100);
        shiningEffect(self.icon, self.shiningAjustPos.x, self.shiningAjustPos.y);
    end
end
